package com.ruoyi.kws.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.kws.bean.PieBean;
import com.ruoyi.kws.bean.RqData;
import com.ruoyi.kws.bean.VueData;
import com.ruoyi.kws.domain.CountData;
import com.ruoyi.kws.domain.CountYm;
import com.ruoyi.kws.mapper.CountDataMapper;
import com.ruoyi.kws.service.ICountDataService;
import com.ruoyi.tool.WfcTool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.time.YearMonth;
import java.util.*;

/**
 * 供应商报表Controller
 *
 * @author wfc
 * @date 2024-03-26
 */
@RestController
@RequestMapping("/kws/bb")
public class BbDayController extends BaseController
{

    @Autowired
    private CountDataMapper countDataMapper;
    /**
     * 初始化首页
     */
    @PutMapping("/initHome")
    public AjaxResult initHome(@RequestBody RqData data) {
        {
            Map<String, Object> result = new HashMap<>(4);
            //把数据放在Map里面推到页面
            CountData ret = new CountData();
            ret.setRq(data.getRq());
            //日期来自页面
            List<CountData>  countDataList = countDataMapper.selectCountDataList(ret);

            VueData vueData = new VueData();
            //游客量
            List<PieBean> pie01s = new ArrayList<>();
            //门票
            List<PieBean> pie02s = new ArrayList<>();
            //二消
            List<PieBean> pie03s = new ArrayList<>();

            //检索出当前日期的统计数据并设置到页面
            if (countDataList.size() > 0) {
                CountData countData = countDataList.get(0);
                //列表数字
                vueData.setTourists(countData.getTourists());
                vueData.setMultiplePurpose(countData.getMultiplePurpose());
                vueData.setGateMoney(countData.getGateMoney());
                vueData.setParkade(countData.getParkade());
                vueData.setSecondRevenue(countData.getSecondRevenue());

                //pie01s
                PieBean ret01 = new PieBean(countData.getSwipe(), "刷卡");
                pie01s.add(ret01);
                ret01 = new PieBean(countData.getBuyTickets(), "买票");
                pie01s.add(ret01);
                ret01 = new PieBean(countData.getFreeTicket(), "免票");
                pie01s.add(ret01);

                //pie02s
                PieBean ret02 = new PieBean(countData.getOnlineAdult(), "成人");
                pie02s.add(ret02);
                ret02 = new PieBean(countData.getOnlineChildren(), "儿童");
                pie02s.add(ret02);
                ret02 = new PieBean(countData.getOnlineDouble(), "双人");
                pie02s.add(ret02);
                ret02 = new PieBean(countData.getOnlineBigSmall(), "一大一小");
                pie02s.add(ret02);
                ret02 = new PieBean(countData.getOnlineTwoBigSmall(), "两大一小");
                pie02s.add(ret02);
                ret02 = new PieBean(countData.getOfflineAdult(), "线下成人");
                pie02s.add(ret02);
                ret02 = new PieBean(countData.getOfflineChildren(), "线下儿童");
                pie02s.add(ret02);

                //pie03s
                PieBean ret03 = new PieBean(countData.getSr1(), "朐茶");
                pie03s.add(ret03);
                ret03 = new PieBean(countData.getSr2(), "五谷园");
                pie03s.add(ret03);
                ret03 = new PieBean(countData.getSr3(), "讲解");
                pie03s.add(ret03);
                ret03 = new PieBean(countData.getSr4(), "祈福牌自助");
                pie03s.add(ret03);
                ret03 = new PieBean(countData.getSr5(), "小吃车");
                pie03s.add(ret03);
                ret03 = new PieBean(countData.getSr6(), "研学团建");
                pie03s.add(ret03);
                ret03 = new PieBean(countData.getSr7(), "草地业态");
                pie03s.add(ret03);
                ret03 = new PieBean(countData.getSr8(), "场地租赁");
                pie03s.add(ret03);
                ret03 = new PieBean(countData.getSr9(), "活动集市");
                pie03s.add(ret03);

            }else{
                //列表数字 设置0
                vueData.setTourists(0L);
                vueData.setMultiplePurpose(new BigDecimal(0));
                vueData.setGateMoney(new BigDecimal(0));
                vueData.setParkade(new BigDecimal(0));
                vueData.setSecondRevenue(new BigDecimal(0));
            }

            result.put("vueData", vueData);
            result.put("pie01s", pie01s);
            result.put("pie02s", pie02s);
            result.put("pie03s", pie03s);

            return success(result);
        }
    }



    /**
     * 初始化报表
     */
    @PutMapping("/initBb")
    public AjaxResult initBb(@RequestBody RqData data) {
        {
            //第一行日统计
            //把数据放在Map里面推到页面
            Map<String, Object> result = new HashMap<>(3);
            // 第一行 barLine
            String ym=data.getYm();
            YearMonth currentYearMonth =YearMonth.parse(ym);
            // 计算这个月有多少天
            int daysInMonth = currentYearMonth.lengthOfMonth();
            //获取日和星期对用
            List<String> DaysAndWeeks = WfcTool.getDaysAndWeek(currentYearMonth.getYear(),currentYearMonth.getMonthValue());
            String[] bar01s_x = DaysAndWeeks.stream().toArray(String[]::new);
            // 〇综合营收
            BigDecimal[] bar01s_0 = new BigDecimal[daysInMonth];
            Arrays.fill(bar01s_0, BigDecimal.ZERO); // 使用Arrays.fill将所有元素初始化为0

            // ①门票
            BigDecimal[] bar01s_1 = new BigDecimal[daysInMonth];
            Arrays.fill(bar01s_1, BigDecimal.ZERO); // 使用Arrays.fill将所有元素初始化为0
            // ②停车场
            BigDecimal[] bar01s_2 = new BigDecimal[daysInMonth];
            Arrays.fill(bar01s_2, BigDecimal.ZERO); // 使用Arrays.fill将所有元素初始化为0
            // ③二消
            BigDecimal[] bar01s_3 = new BigDecimal[daysInMonth];
            Arrays.fill(bar01s_3, BigDecimal.ZERO); // 使用Arrays.fill将所有元素初始化为0
            // ④游客量
            long[] bar01s_4 = new long[daysInMonth];
            Arrays.fill(bar01s_4, 0l); // 使用Arrays.fill将所有元素初始化为0

            List<CountYm> retCountDatas = countDataMapper.selectCountDataList4Bb(ym);
            for(int i=0;i<retCountDatas.size();i++){
                int rq =retCountDatas.get(i).getDay();
                bar01s_x[rq-1]=bar01s_x[rq-1]+"\n"+(retCountDatas.get(i).getTq()==null ? "":retCountDatas.get(i).getTq());
                bar01s_0[rq-1]=retCountDatas.get(i).getMultiplePurpose();
                bar01s_1[rq-1]=retCountDatas.get(i).getGateMoney();
                bar01s_2[rq-1]=retCountDatas.get(i).getParkade();
                bar01s_3[rq-1]=retCountDatas.get(i).getSecondRevenue();
                bar01s_4[rq-1]=retCountDatas.get(i).getTourists();
            }

            result.put("bar01s_x", bar01s_x);
            result.put("bar01s_0", bar01s_0);
            result.put("bar01s_1", bar01s_1);
            result.put("bar01s_2", bar01s_2);
            result.put("bar01s_3", bar01s_3);
            result.put("bar01s_4", bar01s_4);


            //TODO 第二行月统计
            List<CountYm> countByyears = countDataMapper.selectCountYmList4Bb1(data.getYear());
            //12个月金额初始化
            BigDecimal[] mutipeBar01_0 = new BigDecimal[12];
            Arrays.fill(mutipeBar01_0, BigDecimal.ZERO); // 使用Arrays.fill将所有元素初始化为0

            BigDecimal[] mutipeBar01_1 = new BigDecimal[12];
            Arrays.fill(mutipeBar01_1, BigDecimal.ZERO); // 使用Arrays.fill将所有元素初始化为0

            BigDecimal[] mutipeBar01_2 = new BigDecimal[12];
            Arrays.fill(mutipeBar01_2, BigDecimal.ZERO); // 使用Arrays.fill将所有元素初始化为0

            BigDecimal[] mutipeBar01_3 = new BigDecimal[12];
            Arrays.fill(mutipeBar01_3, BigDecimal.ZERO); // 使用Arrays.fill将所有元素初始化为0

            long[] mutipeBar01_4 = new long[12];
            Arrays.fill(mutipeBar01_4, 0l); // 使用Arrays.fill将所有元素初始化为0
            //综合营收', '门票', '停车场', '二消'
            for (int i = 0; i < countByyears.size(); i++) {
                int month = countByyears.get(i).getMonth();
                mutipeBar01_0[month - 1] = countByyears.get(i).getMultiplePurpose();
                mutipeBar01_1[month - 1] = countByyears.get(i).getGateMoney();
                mutipeBar01_2[month - 1] = countByyears.get(i).getParkade();
                mutipeBar01_3[month - 1] = countByyears.get(i).getSecondRevenue();
                mutipeBar01_4[month - 1] = countByyears.get(i).getTourists();
            }
            result.put("mutipeBar01_0", mutipeBar01_0);
            result.put("mutipeBar01_1", mutipeBar01_1);
            result.put("mutipeBar01_2", mutipeBar01_2);
            result.put("mutipeBar01_3", mutipeBar01_3);
            result.put("mutipeBar01_4", mutipeBar01_4);

            //TODO 第三行年统计
            List<CountYm> countByNulls = countDataMapper.selectCountYmList4Bb2();
            int countYear=countByNulls.size();

            //每年金额初始化
            //xAxis的年集合
            Integer[] mutipeBar02_x = new Integer[countYear];

            BigDecimal[] mutipeBar02_0 = new BigDecimal[countYear];
            Arrays.fill(mutipeBar02_0, BigDecimal.ZERO); // 使用Arrays.fill将所有元素初始化为0

            BigDecimal[] mutipeBar02_1 = new BigDecimal[countYear];
            Arrays.fill(mutipeBar02_1, BigDecimal.ZERO); // 使用Arrays.fill将所有元素初始化为0

            BigDecimal[] mutipeBar02_2 = new BigDecimal[countYear];
            Arrays.fill(mutipeBar02_2, BigDecimal.ZERO); // 使用Arrays.fill将所有元素初始化为0

            BigDecimal[] mutipeBar02_3 = new BigDecimal[countYear];
            Arrays.fill(mutipeBar02_3, BigDecimal.ZERO); // 使用Arrays.fill将所有元素初始化为0
            long[] mutipeBar02_4 = new long[countYear];
            Arrays.fill(mutipeBar02_4, 0l); // 使用Arrays.fill将所有元素初始化为0

            for (int i = 0; i < countByNulls.size(); i++) {
                mutipeBar02_x[i] =countByNulls.get(i).getYear();
                mutipeBar02_0[i] = countByNulls.get(i).getMultiplePurpose();
                mutipeBar02_1[i] = countByNulls.get(i).getGateMoney();
                mutipeBar02_2[i] = countByNulls.get(i).getParkade();
                mutipeBar02_3[i] = countByNulls.get(i).getSecondRevenue();
                mutipeBar02_4[i] = countByNulls.get(i).getTourists();
            }
            result.put("mutipeBar02_x", mutipeBar02_x);
            result.put("mutipeBar02_0", mutipeBar02_0);
            result.put("mutipeBar02_1", mutipeBar02_1);
            result.put("mutipeBar02_2", mutipeBar02_2);
            result.put("mutipeBar02_3", mutipeBar02_3);
            result.put("mutipeBar02_4", mutipeBar02_4);
            return success(result);
        }
    }

}
